﻿using Fuse8.ContentManagementFramework.Domain;
using Fuse8.DomainFramework.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Fuse8.ContentManagementFramework.Administration.Models
{
    [Serializable]
    public class CmsAdminMediaActionModel
    {
        public const string ActionDelete = "delete";
        public const string MediaContentIdentificatorSplitter = ":";
        public const string MediaContentIdentificatorFormat = "{0}:{1}";

        public int Id { get; set; }

        public IList<string> MediaContent { get; set; }

        public string Action { get; set; }
    }

    [Serializable]
    public class CmsAdminMediaModel : CmsSiteAwareModel
    {
        public IEnumerable<dynamic> AllMedia { get; set; }

        public int SelectedValue { get; set; }

        public CmsAdminMediaModel()
        { }

        public CmsAdminMediaModel(IList<CmsMedia> allMedia, int? selectedId)
        {
            Guard.ArgumentNotNull(allMedia, "allMedia");

            allMedia.Insert(0, new CmsMedia { FolderName = "Create a new folder" });

            AllMedia = new SelectList(allMedia.Select(e => new { e.Id, FolderName = FormatLevel(e.FolderName, e.Level) }), "Id", "FolderName");

            SelectedValue = selectedId.HasValue ? selectedId.Value : 0;
        }

        private static string FormatLevel(string text, int level)
        {
            return text.PadLeft(text.Length + level, '\u00a0');
        }
    }

    [Serializable]
    public class CmsAdminMediaSaveModel : CmsSiteAwareModel
    {
        [Required(ErrorMessage = "Folder can't have an empty name")]
        public string FolderName { get; set; }

        public string Description { get; set; }

        public int? ParentMediaId { get; set; }

        public string Delete { get; set; }

        public CmsMedia ConvertToMedia()
        {
            return new CmsMedia()
            {
                Id = Id,
                FolderName = FolderName,
                Description = Description,
                ParentMediaId = ParentMediaId,
                SiteCategory = SiteCategory
            };
        }
    }
}